#!/bin/bash
#TIME：2020-07-28
#exporter的端口号
#添加node到promrtheus配置文件的脚本
#editor by yhzong2
nodeport=9100
prometheus_dir=/data/gpe/prometheus
read -p "请输入需要加入prometheus的网段: " subnet
#将用户输入的网段信息输入到subnet.txt的文本中
echo $subnet >subnet.txt
#抓取网络的前三位例如:192.168.130
subnetip=`cat subnet.txt |awk -F "." '{print $1"."$2"."$3}'`
#进行curl检测。端口号为9100,检测如果返回值为200，则记录到nodeip.txt里面
for ip in {1..254};do
        curl -I -m 10 -o /dev/null -s -w %{http_code}  ${subnetip}.${ip}:${nodeport} >/dev/null
        if [ $? -eq 200 ];then
                echo ${subnetip}.${ip} >> nodeip.txt
        fi
done
#获取经过检测的。已经安装了的，且9100正常通信的ip地址
cat nodeip.txt|while read nodeip
  do
   cd $prometheus_dir
cat >> prometheus.yml <<EOF
  - job_name: '${nodeip}'
    static_configs:
    - targets: ['${nodeip}:${nodeport}']
EOF
done

